PepSim.mutateDna = function (dna) {
	var newDna = {}, i, luck;
	for (i in dna) {
		luck = PepSim.rand(10);
		if (luck === 0 && dna[i] < 255) {
			newDna[i] = dna[i] + 10;
		} else if (luck === 1 && dna[i] > 10) {
			newDna[i] = dna[i] - 10;
		} else {
			newDna[i] = dna[i];
		}
	}
	return newDna;
};

PepSim.define('PepSim.World', {
	
	pepNum: 100,

	foodNum: 100,
	
	init: function (width, height) {
		this.width = width;
		this.height = height;

		this.entities = new PepSim.List();
		this.createMatrix();
		//this.createPeps();
		//this.createFood();
	},
	
	createFood: function () {
		var i, left = 0, top = 0, food;
		for (i = 0; i < this.foodNum; i = i + 1) {
			left = PepSim.rand(this.width);
			top = PepSim.rand(this.height);
			if (!this.get(left, top)) {
				food = new PepSim.Food(this, left, top);
			}
		}
	},
	
	createPeps: function () {
		var i, left = 0, top = 0, pep;
		for (i = 0; i < this.pepNum; i = i + 1) {
			left = PepSim.rand(this.width);
			top = PepSim.rand(this.height);
			// TODO: pep can raise on top of a cell of food
			// in that case pep eat all the food
			if (!this.get(left, top)) {
				pep = new PepSim.Pep(this, left, top);
			}
		}
	},
	
	createItem : function (x, y, type) {
		var item;
		if (!this.get(x, y)) {
			if (type === 'pep') {
				item = new PepSim.Pep(this, x, y);
			} else if (type === 'food') {
				item = new PepSim.Food(this, x, y);
			} else {
				item = new PepSim.Feeder(this, x, y);
			}
		}
	},
	
	createMatrix: function () {
		var i;
		this.matrix = [];
		for (i = 0; i < this.width; i = i + 1) {
			this.matrix[i] = [];
		}
	},
	

	play : function () {
		this.entities.foreach(function (entity) {
			if (entity.play) {
				entity.play();
			}
		}, this);
		/*
		var node = this.peps.reset(),
			pep;
		while (node) {
			pep = node.value;
			pep.move();
			if (pep.phantom === true) {
				this.clear(pep.left, pep.top);
				this.peps.remove();
				node = this.peps.current;
			} else {
				node = node.next;
			}
		}
		*/
	},
	
	put : function (x, y, o) {
		this.matrix[Math.floor(x)][Math.floor(y)] = o;
	},
	
	get : function (x, y) {
		return this.matrix[Math.floor(x)][Math.floor(y)];
	},
	
	clear : function (x, y) {
		this.matrix[Math.floor(x)][Math.floor(y)] = undefined;
	}

});